﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MailMerge_Generator
{
    public partial class Frm_Config2 : Form
    {
        public Frm_Config2()
        {
            InitializeComponent();
            chk_HasTemplate.Checked = Frm_Main.oLetter.HasTemplate;
            txt_bk_Greeting.Text = Frm_Main.oLetter.Greeting.bookmark;
            txt_bk_Body.Text = Frm_Main.oLetter.Body.bookmark;
            txt_bk_Table.Text = Frm_Main.oLetter.bk_Table;
            txt_bk_Summary.Text = Frm_Main.oLetter.Summary.bookmark;
            txt_bk_Closing.Text = Frm_Main.oLetter.Closing.bookmark;
            txt_TemplatePath.Text = Frm_Main.oLetter.TemplateFilePath;
            chk_HasGreetingBK.Checked = Frm_Main.oLetter.Greeting.hasMark;
            chk_HasBodyBK.Checked = Frm_Main.oLetter.Body.hasMark;
            chk_HasTableBK.Checked = Frm_Main.oLetter.bk_HasTable;
            chk_HasSummaryBK.Checked = Frm_Main.oLetter.Summary.hasMark;
            chk_HasClosingBK.Checked = Frm_Main.oLetter.Closing.hasMark;
        }
        private void btn_close_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void btn_SaveFields_Click(object sender, EventArgs e)
        {
            Frm_Main.oLetter.Greeting.bookmark = txt_bk_Greeting.Text;
            Frm_Main.oLetter.Body.bookmark = txt_bk_Body.Text;
            Frm_Main.oLetter.bk_Table = txt_bk_Table.Text;
            Frm_Main.oLetter.Summary.bookmark = txt_bk_Summary.Text;
            Frm_Main.oLetter.Closing.bookmark = txt_bk_Closing.Text;
            Frm_Main.oLetter.Sender.bookmark = txt_bk_Closing.Text;
            Frm_Main.oLetter.TemplateFilePath = txt_TemplatePath.Text;
            Frm_Main.oLetter.Greeting.hasMark = chk_HasGreetingBK.Checked;
            Frm_Main.oLetter.Body.hasMark = chk_HasBodyBK.Checked;
            Frm_Main.oLetter.bk_HasTable = chk_HasTableBK.Checked;
            Frm_Main.oLetter.Summary.hasMark = chk_HasSummaryBK.Checked;
            Frm_Main.oLetter.Closing.hasMark = chk_HasClosingBK.Checked;
            Frm_Main.oLetter.Sender.hasMark = chk_HasClosingBK.Checked;
            Close();
        }
        private void btn_SelectTemplate_Click(object sender, EventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "Select template file";
            fdlg.Filter = "All files (*.*)|*.*|Word Templates (*.dotx)(*.dot)|*.dotx;*.dot";
            fdlg.FilterIndex = 2;
            fdlg.RestoreDirectory = true;
            fdlg.Multiselect = false;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                txt_TemplatePath.Text = fdlg.FileName;
            }
            else
            {
                //do nothing
            }
        }
        private void BoxChecked(object sender, EventArgs e)
        {
            CheckBox chk = (CheckBox)sender;
            switch (chk.Name)
            {
                case "chk_HasTemplate":
                    Frm_Main.oLetter.HasTemplate = chk.Checked;
                    grp_Bookmarks.Enabled = chk.Checked;
                    txt_TemplatePath.Enabled = chk.Checked;
                    btn_SelectTemplate.Enabled = chk.Checked;
                    chk_HasGreetingBK.Checked = chk.Checked;
                    chk_HasBodyBK.Checked = chk.Checked;
                    chk_HasTableBK.Checked = chk.Checked;
                    chk_HasSummaryBK.Checked = chk.Checked;
                    chk_HasClosingBK.Checked = chk.Checked;
                    break;
                case "chk_HasGreetingBK":
                    txt_bk_Greeting.Enabled = chk.Checked;
                    break;
                case "chk_HasBodyBK":
                    txt_bk_Body.Enabled = chk.Checked;
                    break;
                case "chk_HasTableBK":
                    txt_bk_Table.Enabled = chk.Checked;
                    break;
                case "chk_HasSummaryBK":
                    txt_bk_Summary.Enabled = chk.Checked;
                    break;
                case "chk_HasClosingBK":
                    txt_bk_Closing.Enabled = chk.Checked;
                    break;
                default:
                    break;
            }
        }
    }
}
